package socket;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class client {
    private Socket socket;
    public client() {
        try {
            System.out.println("正在连接服务器");
             socket = new Socket("localhost", 12399);
            System.out.println("与服务器建立链接成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start() {
        try {
            OutputStream osw = socket.getOutputStream();
            OutputStreamWriter out = new OutputStreamWriter(osw, StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(out);
            PrintWriter ow = new PrintWriter(bw, true);
            Scanner sc=new Scanner(System.in);
            while(true){
                String line=sc.nextLine();
                if ("exit".equals(line)){
                    break;
                }
                ow.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        client cli = new client();
        cli.start();
    }
}
